<template>
  <div class="search-bar-box">
    <slot name="input">
      <el-input
        v-if="type=='input'"
        class="search-input"
        clearable
        v-model="searchInput"
        @change="handleChange"
        :placeholder="placeholder"
      ></el-input>
      <el-select
        v-if="type=='select'"
        class="search-input"
        clearable
        filterable
        v-model="searchInput"
        @change="handleChange"
        :placeholder="placeholder"
      >
        <el-option
          v-for="item in options"
          :key="props.value?item[props.value]:item.baseId"
          :label="props.label?item[props.label]:item.baseName"
          :value="props.value?item[props.value]:item.baseId"
        >
        </el-option>
      </el-select>
    </slot>
    <a class="search-btn" :disabled="disabled" @click="handleSearch">搜索</a>
  </div>

</template>

<script>
export default {
  name: 'searchBar',
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    value: {
      type: String,
      default: ''
    },
    placeholder: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    },
    type: {
      type: String,
      default: 'input'
    },
    options: {
      type: Array,
      default: () => {
        return []
      }
    },
    props: {
      label: 'baseName',
      value: 'baseId'
    }
  },
  data() {
    return {
      searchInput: ''
    }
  },
  watch: {
    options:{
      handler(val) {
      },
      deep:true
    },
    searchInput(val){
    },
    value: {
      handler(val) {
        this.searchInput = val
      },
      immediate:true
    }
  },
  methods: {
    handleChange(val) {
      this.$emit('change', val)
    },
    handleSearch() {
      this.$emit('search', this.searchInput)
    }
  }
}
</script>

<style scoped lang="scss">
.search-bar-box {
  height: 50px;
  width: 700px;
  margin: 30px auto;
  position: relative;
  overflow: hidden;
  border-radius: 50px;

  ::v-deep .el-select {
    width: 100%;
  }

  ::v-deep .el-input {
    width: 100%;
  }

  .search-input {
    font-size: 14px;
    color: #999;
    width: 100%;
    height: 50px;
    line-height: 50px;

    ::v-deep .el-input__inner {
      height: 50px;
      line-height: 50px;
      border-radius: 50px;
      border: 2px solid #3184FF;
      box-sizing: border-box;
      padding-left: 44px;
      padding-right: 170px;
      background: url(~@/assets/images/search-icon.png) no-repeat 10px center;
      background-size: 14px 14px;
    }

    ::v-deep .el-input__suffix {
      margin-right: 140px;
    }
  }

  .search-btn {
    width: 140px;
    height: 50px;
    line-height: 50px;
    border-radius: 50px;
    background-color: #3184FF;
    position: absolute;
    right: 0;
    top: 0;
    text-align: center;
    color: #fff;
    font-size: 18px;
    font-weight: bold;
    //cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style>
